/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.pixlib.sfe.view.editor 
{
	import net.pixlib.plugin.Plugin;
	import net.pixlib.sfe.display.FontPanel;
	import net.pixlib.sfe.event.EventList;
	import net.pixlib.sfe.event.FontEvent;
	import net.pixlib.sfe.event.LibraryEvent;
	import net.pixlib.sfe.log.Debug;
	import net.pixlib.sfe.util.getTranslation;
	import net.pixlib.sfe.view.ViewList;
	import net.pixlib.sfe.view.abstract.AsWingView;
	import net.pixlib.tms.LocalManager;
	import net.pixlib.tms.LocalManagerEvent;
	import net.pixlib.utils.MathUtils;

	import org.aswing.ASColor;
	import org.aswing.AsWingConstants;
	import org.aswing.BorderLayout;
	import org.aswing.Component;
	import org.aswing.Insets;
	import org.aswing.JButton;
	import org.aswing.JMenuBar;
	import org.aswing.JPanel;
	import org.aswing.JScrollPane;
	import org.aswing.JViewport;
	import org.aswing.SoftBox;
	import org.aswing.SoftBoxLayout;
	import org.aswing.border.EmptyBorder;

	import flash.events.Event;

	/**
	 * Editor view.
	 * 
	 * <p>Manages <code>FontProperties</code> list.</p>
	 * 
	 * @author Romain Ecarnot
	 */
	public class EditorView extends AsWingView
	{
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		/** @private */
		private var _bgColor : ASColor; 

		/** @private */
		private var _mcList : SoftBox; 

		/** @private */
		private var _buttonExport : JButton; 

		/** @private */
		private var _viewPort : JViewport; 

		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Creates view.
		 */		
		public function EditorView(owner : Plugin)
		{
			super(owner, ViewList.EDITOR_VIEW, new JPanel(new BorderLayout(5, 5)));
		}
		
		/**
		 * Triggered when library content has changed.
		 */
		public function onLibraryChanged(event : LibraryEvent) : void
		{
			_mcList.removeAll();
			
			var len : uint = event.getFontList().length;
			var comp : FontPanel;
			
			for(var i : uint = 0;i < len; i++)
			{
				comp = new FontPanel(getOwner(), event.getFontList()[i]);
				_mcList.append(comp);
			}
			
			redraw();
		}

		/**
		 * Triggered when a font has been added in LibraryModel.
		 * 
		 * @see net.pixlib.sfe.model.LibraryModel#addFont
		 */
		public function onFontAdded(event : FontEvent) : void
		{
			var comp : FontPanel = new FontPanel(getOwner(), event.getFont());
			comp.setBackground(_bgColor);
			comp.setFocus();
			
			_mcList.append(comp);
			
			redraw();
			
			_viewPort.scrollToBottomLeft();
		}

		/**
		 * Triggered when a font has been removed from LibraryModel.
		 * 
		 * @see net.pixlib.sfe.model.LibraryModel#removeFont
		 */
		public function onFontRemoved(event : FontEvent) : void
		{
			var com : FontPanel;
			var len : uint = _mcList.getComponentCount();
			
			for(var i : uint = 0;i < len; i++)
			{
				com = _mcList.getComponent(i) as FontPanel;
				if(event.getFont().id == com.id)
				{
					Debug.DEBUG("Remove Font ID = " + com.id, this);
					
					_mcList.remove(com);
					break;
				}
			}
			
			redraw();
		}

		/**
		 * Redraws list layout.
		 */
		public function redraw() : void
		{
			var len : uint = _mcList.getComponentCount();
			var comp : Component;
			
			for(var i : uint = 0;i < len; i++)
			{
				comp = _mcList.getComponent(i);
				comp.setOpaque(MathUtils.isOdd(i));
			}	
		}

		
		//--------------------------------------------------------------------
		// Protected methods
		//--------------------------------------------------------------------
		
		/**
		 * Returns view content as AsWing JPanel.
		 */
		protected function getPanel() : JPanel
		{
			return getComponent() as JPanel;	
		}	

		/**
		 * Inits User Interface.
		 */
		override protected function onInitView( ) : void
		{
			LocalManager.getInstance().addEventListener(LocalManagerEvent.onLocalChangeEVENT, _onLocalChangeHandler);
			
			getComponent().setBorder(new EmptyBorder(null, new Insets(5, 5, 5, 5)));
			
			var m : JMenuBar = new JMenuBar();
			_bgColor = m.getBackground();
			m = null;
			
			_mcList = new SoftBox(SoftBoxLayout.Y_AXIS, 5);
			_mcList.setBorder(new EmptyBorder(null, new Insets(5, 5, 5, 5)));
			
			_viewPort = new JViewport(_mcList, true);
			_viewPort.setVerticalAlignment(AsWingConstants.TOP);
			
			_buttonExport = new JButton("export");
			_buttonExport.setToolTipText("export and compil shared font library");
			_buttonExport.addActionListener(_onExportClickHandler);
			
			getPanel().append(new JScrollPane(_viewPort), BorderLayout.CENTER);
			getPanel().append(_buttonExport, BorderLayout.SOUTH);
			
			_onLocalChangeHandler();
			
			super.onInitView();
		}

		
		//--------------------------------------------------------------------
		// Private methods
		//--------------------------------------------------------------------
		
		/** @private */
		private function _onExportClickHandler(event : Event) : void
		{
			firePrivateEvent(new Event(EventList.EXPORT_LIBRARY));
		}

		/** @private */
		private function _onLocalChangeHandler(event : Event = null) : void
		{
			_buttonExport.setText(getTranslation("editorExportButton"));
			_buttonExport.setToolTipText(getTranslation("editorExportButtonTip"));
		}
	}
}
